﻿using Decision.BusinessLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Telerik.Web.UI.GridExcelBuilder;
using thaihua.service;

public partial class report_Report3_3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
        }
    }

    private void InitPage()
    {
        DropDownStaticFactory.GetProvince(ref ddl7_PROVINCE, true);
        Reset_Click(null, null);
    }

    protected override void OnInit(EventArgs e)
    {
        InitValidation();
        base.OnInit(e);
    }

    /// <summary>
    /// Init Validate RequireField in Controll
    /// </summary>
    private void InitValidation()
    {

    }
    protected void Search_Click(object sender, EventArgs e)
    {
        RadGrid1.CurrentPageIndex = 0;
        BindData(null);
    }
    protected void RadGrid1_ExcelMLWorkBookCreated(object sender, Telerik.Web.UI.GridExcelBuilder.GridExcelMLWorkBookCreatedEventArgs e)
    {
        foreach (RowElement row in e.WorkBook.Worksheets[0].Table.Rows)
        {
            row.Cells[0].StyleValue = "Style1";
        }

        StyleElement style = new StyleElement("Style1");
        style.InteriorStyle.Pattern = InteriorPatternType.Solid;
        style.InteriorStyle.Color = System.Drawing.Color.LightGray;
        e.WorkBook.Styles.Add(style);
    }

    protected void ImageButton_Click(object sender, ImageClickEventArgs e)
    {
        string alternateText = (sender as ImageButton).AlternateText;
        ConfigureExport();
        RadGrid1.ExportSettings.Excel.Format = Telerik.Web.UI.GridExcelExportFormat.Html;
        RadGrid1.MasterTableView.ExportToExcel();
    }

    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        ConfigureExport();
    }

    public void ConfigureExport()
    {
        RadGrid1.ExportSettings.FileName = DateTime.Now.ToFileTimeUtc() + "";
        RadGrid1.ExportSettings.ExportOnlyData = true;
        RadGrid1.ExportSettings.IgnorePaging = false;
        RadGrid1.ExportSettings.OpenInNewWindow = true;
    }
    protected void RadGrid2_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        //List<QueryMasTerData> listReport = DCSFactory.CreateQueryMasTerDataFactory().SelectByBusinessObject(new QueryMasTerDataCriteria
        //{
        //    PageNum = 1,
        //    PageSize = 9999999,

        //    PROVINCE_ID = int.Parse(ddl7_PROVINCE.SelectedValue),
        //    START_VOTE_DATE = cal_startVOTE_DATE.SelectedDate,
        //    END_VOTE_DATE = cal_endVOTE_DATE.SelectedDate,
        //    START_RESULT_DATE = cal_startRESULT_DATE.SelectedDate,
        //    END_RESULT_DATE = cal_endRESULT_DATE.SelectedDate,
        //    START_SIGN_DATE = cal_startSIGN_DATE.SelectedDate,
        //    END_SIGN_DATE = cal_endSIGN_DATE.SelectedDate
        //});

        RadGrid1.DataSource = ViewState["listReport"];
    }
    protected void Reset_Click(object sender, EventArgs e)
    {
        Util.ResetForm(Util.GetControlFrom(pnSearch));
        RadGrid1.CurrentPageIndex = 0;
        BindReport(null);
    }

    private void BindReport(List<QueryMasTerData> listReport)
    {
        ViewState["listReport"] = listReport;

        bool display = listReport != null && listReport.Count > 0;

        if (display)
        {
            Util.jsWriter(Page, "$('#exportXLS').show();");
        }
        else
        {
            Util.jsWriter(Page, "$('#exportXLS').hide();");
        }

        RadGrid1.Visible = display;
        RadGrid1.DataSource = listReport;
        RadGrid1.VirtualItemCount = listReport == null || listReport.Count == 0 ? 0 : (int)listReport[0].RowAll;
        RadGrid1.DataBind();
    }

    protected void RadGrid1_PageIndexChanged(object sender, GridPageChangedEventArgs e)
    {
        RadGrid1.CurrentPageIndex = e.NewPageIndex;
        this.BindData(null);
    }
    protected void RadGrid1_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
    {
        RadGrid1.CurrentPageIndex = 0;
        this.BindData(e.NewPageSize);
    }

    private void BindData(int? p)
    {
        List<QueryMasTerData> listReport = DCSFactory.CreateQueryMasTerDataFactory().SelectByBusinessObject(new QueryMasTerDataCriteria
        {
            PageNum = RadGrid1.CurrentPageIndex + 1,
            PageSize = p != null ? p : RadGrid1.PageSize,

            VOTE_TYPE_ID = "3",//สมาชิกสภาท้องถิ่นหรือผู้บริหารท้องถิ่น(สถ./ผถ.)

            PROVINCE_ID = int.Parse(ddl7_PROVINCE.SelectedValue),
            START_VOTE_DATE = cal_startVOTE_DATE.SelectedDate,
            END_VOTE_DATE = cal_endVOTE_DATE.SelectedDate,
            START_RESULT_DATE = cal_startRESULT_DATE.SelectedDate,
            END_RESULT_DATE = cal_endRESULT_DATE.SelectedDate,
            START_SIGN_DATE = cal_startSIGN_DATE.SelectedDate,
            END_SIGN_DATE = cal_endSIGN_DATE.SelectedDate
        });


        BindReport(listReport);
    }
}